package com.tangding.hprose.api.server.command.impl.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tangding.common.exception.GException;
import com.tangding.common.tools.ErrorConstants;
import com.tangding.hprose.api.server.command.Command;
import com.tangding.hprose.api.server.command.Response;
import com.tangding.net.pool.BaseNettySessionPool;
import com.tangding.service.dao.user.UserRepository;
import com.tangding.service.entity.user.User;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年12月14日_上午9:27:13
 *
 */
@Component
public class DownlineAdmins implements Command {

	@Autowired
	BaseNettySessionPool pool;
	
	@Autowired
	UserRepository userRepository;
	
	@Override
	public Response exe(Object... params) throws GException {
		/*Collection<String> onlineUids = pool.onlines();
		
		List<Map<String, Object>> list = new ArrayList<>();
		if (onlineUids != null) {
			onlineUids.forEach(uid -> {
				User user = userRepository.getUserByUid(uid);
				
				Map<String, Object> map = new HashMap<>();
				map.put("uid", uid);
				map.put("name", user.getName());
				list.add(map);
			});
		}*/
		
		List<Object> list = new ArrayList<>();
		@SuppressWarnings("unchecked")
		Map<String, Object> userRequest = (Map<String, Object>) params[params.length - 1];
		String uid = (String) userRequest.get("uid");
		
		User user = userRepository.getUserByUid(uid);
		if (user == null) {
			throw new GException(ErrorConstants.USER_NOT_FOUND);
		}
		
		List<User> users = new ArrayList<>();
		users.add(user);
		
		GetUsers.findChildUser(uid, users);
		
		users.forEach(u -> {
			Map<String, Object> map = new HashMap<>();
			map.put("uid", u.getUid());
			map.put("name", u.getName());
			list.add(map);
		});
		
		return doResponseSucc(list);
	}

}
